package com.yuanzi.base.auth;

import java.util.Map;

import org.springframework.stereotype.Service;

import com.yuanzi.base.constants.AuthConstants;
import com.yuanzi.base.redis.OuterHashRedisService;
import com.yuanzi.base.util.StringUtils;

/**
 * 存储学生对于教学班的从属关系(只限于邀请码授权)
 * Hash形式：zuul-service:member_teachclass:memberId（key）:teachClassId（field）=>权限String用逗号分隔（value）
 * 1.主讲教师 2.助教 3.创建人 4.学生
 * @author cui
 * @data 2017年9月20日
 *
 */
@Service
public class AuthMemberTeachclassRedisService {
	
	private static String serviceName = AuthConstants.AUTH_SERVICE_NAME;
	
	/**
	 * 设置成员在这个教学班中的权限
	 * @author cui
	 * @param memberId
	 * @param teachClassId
	 * @param right
	 */
	public static void setRightOfMemberInTeachClass(String memberId, String teachClassId, String right){
		String oldRight = OuterHashRedisService.hget(serviceName, AuthConstants.REDIS_MEMBER_TEACHCLASS+memberId, teachClassId);
		if(!StringUtils.isEmpty(oldRight)){
			right += ","+oldRight;
		}
		OuterHashRedisService.hset(serviceName, AuthConstants.REDIS_MEMBER_TEACHCLASS+memberId, teachClassId, right);
	}
	
	/**
	 * 批量设置成员在这个教学班中权限(覆盖)
	 * @author cui
	 * @param memberId
	 * @param teachClassRightMap
	 */
	public static void setRightOfMemberInTeachClassInBatch(String memberId, Map<String, String> teachClassRightMap){
		OuterHashRedisService.hmset(serviceName, AuthConstants.REDIS_MEMBER_TEACHCLASS+memberId, teachClassRightMap);
	}
	
	/**
	 * 批量删除成员对特定教学班权限
	 * @author cui
	 * @param teachClassId
	 * @param memberIds
	 */
	public static void delRightOfStudentInTeachclass(String teachClassId, String... memberIds){
		if(memberIds.length == 0){
			return ;
		}
		for(String memberId : memberIds){
			OuterHashRedisService.hdel(serviceName, AuthConstants.REDIS_MEMBER_TEACHCLASS+memberId, teachClassId);
		}
	}
	
	/**
	 * 删除这个成员对教学班所有权限
	 * @author cui
	 * @param memberId
	 */
	public static void delRightOfMemberInTeachclassAll(String memberId){
		OuterHashRedisService.del(serviceName, AuthConstants.REDIS_MEMBER_TEACHCLASS+memberId);
	}
	
	/**
	 * 删除这个成员对特定教学班权限
	 * @author cui
	 * @param memberId
	 * @param teachClassId
	 */
	public static void delRightOfMemberInTeachclass(String memberId, String teachClassId){
		OuterHashRedisService.hdel(serviceName, AuthConstants.REDIS_MEMBER_TEACHCLASS+memberId, teachClassId);
	}
	
	/**
	 * 判断该成员是否是属于该教学班的学生
	 * @author cui
	 * @param memberId
	 * @param teachClassId
	 * @return
	 */
	public static boolean isStudentInTeachclass(String memberId, String teachClassId){
		String right = getRightOfMemberInTeachclass(memberId, teachClassId);
		if(StringUtils.isEmpty(right)){
			return right.contains(AuthConstants.REDIS_MEMBER_TEACHCLASS);
		}
		return false;
	}
	
	/**
	 * 获取该成员在教学班
	 * @author cui
	 * @param memberId
	 * @param teachClassId
	 * @return
	 */
	public static String getRightOfMemberInTeachclass(String memberId, String teachClassId){
		return OuterHashRedisService.hget(serviceName, AuthConstants.REDIS_MEMBER_TEACHCLASS+memberId, teachClassId);
	}
	
	/**
	 * 没有权限时设置默认值
	 * @param memberId
	 * @param teachClassId
	 */
	public static void setNoneRightOfMemeberInTeachclass(String memberId, String teachClassId) {
		OuterHashRedisService.hset(serviceName, AuthConstants.REDIS_MEMBER_TEACHCLASS+memberId, teachClassId, AuthConstants.TEACHCLASS_RIGHT_NONE);
	}
}
